﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Model;
using System.Text.RegularExpressions;

namespace University.Workshop.CreditCard
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BankAccount bankAccount = new BankAccount();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string Code = textBoxCode.Text;
            string Name = textBoxUserName.Text;
            bool result = false;
            string resultNewUser = "";
            if (radioButtonLogIn.IsChecked == true)
            {
                if (Name != "" && Code != "")
                {
                    result = bankAccount.ConsultLogOn(Name, Code);
                    if (result)
                    {

                        radioButtonConsultAmount.Visibility = Visibility.Visible;
                        radioButtonDepositMoney.Visibility = Visibility.Visible;
                        radioButtonRemoveMoney.Visibility = Visibility.Visible;
                        textBoxAmount.Visibility = Visibility.Visible;
                        buttonConsult.Visibility = Visibility.Visible;
                        label3.Visibility = Visibility.Visible;



                        textBoxUserName.Visibility = Visibility.Hidden;
                        textBoxCode.Visibility = Visibility.Hidden;
                        radioButtonLogIn.Visibility = Visibility.Hidden;
                        radioButtonRegisterNewUser.Visibility = Visibility.Hidden;
                        button1.Visibility = Visibility.Hidden;
                        label1.Visibility = Visibility.Hidden;
                        label2.Visibility = Visibility.Hidden;

                    }
                    else
                    {
                        label5.Content = "User Does not Found";
                        textBoxUserName.Text = " ";
                        textBoxCode.Text = " ";
                    }
                }
                else
                {
                    label5.Content = "User Does not Found";
                }//
            }
            else
            {
                if (radioButtonRegisterNewUser.IsChecked == true)
                {
                    if (Name != "" && Code != "")
                    {
                        resultNewUser = bankAccount.RegisterCreditCard(Name, Code);
                        if (resultNewUser=="ok")
                        {

                            radioButtonConsultAmount.Visibility = Visibility.Visible;
                            radioButtonDepositMoney.Visibility = Visibility.Visible;
                            radioButtonRemoveMoney.Visibility = Visibility.Visible;
                            textBoxAmount.Visibility = Visibility.Visible;
                            buttonConsult.Visibility = Visibility.Visible;
                            label3.Visibility = Visibility.Visible;



                            textBoxUserName.Visibility = Visibility.Hidden;
                            textBoxCode.Visibility = Visibility.Hidden;
                            radioButtonLogIn.Visibility = Visibility.Hidden;
                            radioButtonRegisterNewUser.Visibility = Visibility.Hidden;
                            button1.Visibility = Visibility.Hidden;
                            label1.Visibility = Visibility.Hidden;
                            label2.Visibility = Visibility.Hidden;

                        }
                        else
                        {
                            label5.Content = "User Does not Found";
                            textBoxUserName.Text = " ";
                            textBoxCode.Text = " ";
                        }
                    }
                    else
                    {
                        label5.Content = "User Does not Found";
                    }
                }
                else
                { 
                
                }
            }
        }
        public bool IsNumber(string inputvalue)
        {
            Regex isnumber = new Regex("[^0-9]");
            return !isnumber.IsMatch(inputvalue);
        }
        private void buttonConsult_Click(object sender, RoutedEventArgs e)
        {
            string Code = textBoxCode.Text;
            string Name = textBoxUserName.Text;
            string Amount = textBoxAmount.Text;
            string result = "";
            if (radioButtonConsultAmount.IsChecked == true)
            {
                result = Convert.ToString( bankAccount.Consult(Name,Code) );
                if (result == "Doesn'tFound")
                    label4.Content = result;
                else
                    label4.Content = "Amount " + result;
            }
            else
            {
                if (radioButtonRemoveMoney.IsChecked == true && textBoxAmount.Text != "" && IsNumber(Amount))
                {
                    result = bankAccount.ChangeAmount(Code, Amount, "res", Name);
                    if (result == "false")
                        label4.Content = result;
                    else
                    {
                        label4.Content = "Successful Purchase ( " + result + " )";
                        // user.chips =user.chips - Convert.ToInt64(Amount);  //modificarrrrrrrrrrrrrrrrrrrrrrr
                    }
                }
                else
                {
                    if (radioButtonDepositMoney.IsChecked == true && textBoxAmount.Text != "" && IsNumber(Amount))
                    {
                        result = bankAccount.ChangeAmount(Code, Amount, "sum", Name);
                        if (result == "false")
                            label4.Content = result;
                        else
                        {
                            label4.Content = "successful sale ( " + result + " )";

                        }
                    }
                    else
                    {
                        label4.Content = "The Amount not allowed characters or empty";
                    }
                }

            }
        }

        private void textBoxUserName_TextChanged(object sender, TextChangedEventArgs e)
        {
            label5.Content = "";
            if (textBoxUserName.Text.Contains(" "))
            {
                label5.Content = "Can´t contein spaces or can't be empty";
            }
            else
            {
                if ((textBoxUserName.Text).Length <= 4 || (textBoxUserName.Text).Length > 30)
                {
                    button1.IsEnabled = false;
                    label5.Content = "The User Name is too short or long";
                }
                else
                {
                    if (textBoxCode.Text != "")
                        button1.IsEnabled = true;
                }
            }
        }

        private void textBoxCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            label5.Content = "";
            if (textBoxCode.Text.Contains(" "))
            {
                label5.Content = "Can´t contein spaces or can't be empty";
            }
            else
            {
                if ((textBoxCode.Text).Length > 5)
                {
                    button1.IsEnabled = false;
                    label5.Content = "The Code is too long";
                }
                else
                {
                    if (textBoxUserName.Text != "")
                        button1.IsEnabled = true;
                }
            }
        }
    }
}
